package com.hm.service.config.db;

import com.hm.service.config.db.ds1.MainDataSource;
import com.hm.service.config.db.ds1.RegisterMapper;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FactoryConfig {
    private static final Logger logger = LoggerFactory.getLogger(FactoryConfig.class);

    @Bean(name = {"MainSqlSessionFactory"})
    public SqlSessionFactory mainFactory() {
        logger.warn("MainSqlSessionFactory初始化了");
        MainDataSource dataSource = new MainDataSource();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(environment);
        //开启驼峰规则
        configuration.setMapUnderscoreToCamelCase(true);
        //开启日志
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        //添加mapper
        RegisterMapper.init(configuration);
        return new SqlSessionFactoryBuilder().build(configuration);
    }


}
